package com.lou.springboot.service.impl;

import com.lou.springboot.common.PageResult;
import com.lou.springboot.dao.ReaderMapper;
import com.lou.springboot.dao.ReaderMapperCustom;
import com.lou.springboot.entity.Reader;
import com.lou.springboot.service.ReaderService;
import com.lou.springboot.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReaderServiceImpl implements ReaderService {

    @Autowired
    private ReaderMapperCustom readerMapper;

    @Override
    public PageResult getReaderList(PageUtil pageUtil) {
        Map map = new HashMap(pageUtil);
        List<Reader> readers = readerMapper.getReaderList(map);
        int total = readerMapper.getTotalCount();
        PageResult pageResult = new PageResult(readers, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public Reader getReaderByIp(String ip) {
        return readerMapper.getReaderByIp(ip);
    }

    @Override
    public Reader addReader(String ip) {
        Reader reader = new Reader();
        reader.setIp(ip);
        int readerId = readerMapper.insertSelectiveAndReturnId(reader);
        reader.setId(readerId);
        return reader;
    }
}
